home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_12
/
colvin
/
gc.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-10-10
|
1KB
|
46 lines
////////////////////////////////////////////////////////////
// gc.h
//
// Smart pointers to garbage collected objects.
#ifndef GC_H
#define GC_H
#include "gc_imp.h"
// Allocator for garbage collected objects.
template<class T> struct gc {
static void* allocate(size_t n) throw(bad_alloc) {
void* p= ::operator new(gc_data::min()+n);
return &(new(p) gc_value<T>())->data;
}
static void deallocate(T*) throw() {}
};
// Smart pointers to collectable objects.
template<class X> struct gc_ptr : gc_link {
gc_ptr() : gc_link() {}
gc_ptr(X* p) : gc_link(p) {}
gc_ptr(const gc_ptr& r) : gc_link(r) {}
gc_ptr& operator=(const gc_ptr& r) {
mark_or_copy(r);
return *this;
}
gc_ptr& operator=(X* p) {
set_ptr(p);
return *this;
}
operator X*() const { return (X*)ptr; }
X* operator->() const { return (X*)ptr; }
X& operator[](size_t i) { return *((X*)ptr+i); }
X operator[](size_t i) const { return *((X*)ptr+i); }
};
// Do garbage full or partial collection.
void gc_all();
void gc_min();
void gc_handler() throw(bad_alloc);
#endif